package system;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;

import com.solr.web.utils.UUIDS;

public class ThreadJdbc extends Thread{
	
	Connection conn_source_t=null;
	
	static	String url_source="jdbc:mysql://localhost:3306/zntz?user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
	// 线程计数器  
    static private int threadCounts;  
    // 线程名称池  
    static private String threadNames[];  
    
    static {  
        // 假设这里允许系统同时运行最大线程数为5个  
        int maxThreadCounts = 10;  
        threadNames = new String[maxThreadCounts];  
        // 初始化线程名称池  
        for (int i = 1; i <= maxThreadCounts; i++) {  
            threadNames[i - 1] = "子线程_" + i;  
        }  
    }  
    
    public ThreadJdbc() {  
        // 临界资源锁定  
        synchronized (ThreadJdbc.class) {  
            // 线程总数加1  
            threadCounts++;  
            // 从线程名称池中取出一个未使用的线程名  
            for (int i = 0; i < threadNames.length; i++) {  
                if (threadNames[i] != null) {  
                    String temp = threadNames[i];  
                    // 名被占用后清空  
                    threadNames[i] = null;  
                    // 初始化线程名称  
                    this.setName(temp);  
                    break;  
                }  
            }  
        }  
    }  
  
    public void run() {  
        System.out.println("-->>" + this.getName() + "开始运行");  
        try {  
        	long start = System.currentTimeMillis();
    		Class.forName("com.mysql.jdbc.Driver");
    			conn_source_t = DriverManager.getConnection(url_source);
    			String sql="insert into a values(?,?,?,?,?,?,?,?,?)";
    			PreparedStatement ps = conn_source_t.prepareStatement(sql);
    			String name[]={"赵钱孙李","周吴郑王","冯陈诸卫","蒋沈韩杨","朱秦尤许",
    				       "何吕施张","孔曹严华","金魏陶姜","戚谢邹喻","柏水窦章",
    				       "云苏潘葛","奚范彭郎","鲁韦昌马","苗凤花方","俞任袁柳",
    				       "酆鲍史唐","费廉岑薛","雷贺倪汤","滕殷罗毕","郝邬安常",
    				       "乐于时傅","皮卡齐康","伍余元卜","顾孟平黄","和穆萧尹",
    				       "姚邵堪汪"};
    			String dept[]={"研发部","运营部","人事部","市场部","销售部"};
    			double salary = 1000.45;
    			String address ="北京市石景山苹果园西京二区31号楼504";
    			String id_carde = "371623198301265159";
    			String phone="13333333333";
    			String education[] = {"本科","专科","研究生","博士","高中"};
    			String native_place="辽宁省沈阳市皇姑区新华路文明小区41号楼501号";
    			Random r=new Random();
    			for(int i=1;i<=2000;i++){
    				String id=UUIDS.getUUID();
    				ps.setString(1, id);
    				ps.setString(2, dept[r.nextInt(5)]);
    				ps.setString(3, name[r.nextInt(26)]);
    				ps.setDouble(4, (salary+r.nextInt(100)));
    				ps.setString(5, address);
    				ps.setString(6, id_carde);
    				ps.setString(7, phone);
    				ps.setString(8, education[r.nextInt(5)]);
    				ps.setString(9, native_place);
    				ps.addBatch();
    				if(i%500==0){
    					ps.executeBatch();
    				}
    			}
    			long end = System.currentTimeMillis();
    			System.out.println(this.getName()+"耗时："+(end-start)/1000 +"秒");
        } catch (Exception e) {  
            System.out.println(e);  
        } finally {  
            synchronized (ThreadJdbc.class) {  
            	try {
					conn_source_t.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
                // 线程运行完毕后减1  
                threadCounts--;  
                // 释放线程名称  
                String[] threadName = this.getName().split("_");  
                // 线程名使用完后放入名称池  
                threadNames[Integer.parseInt(threadName[1]) - 1] = this.getName();  
                /* 
                 * 通知其他被阻塞的线程，但如果其他线程要执行，则该同步块一定要运行结束（即直 
                 * 到释放占的锁），其他线程才有机会执行，所以这里的只是唤醒在此对象监视器上等待 
                 * 的所有线程，让他们从等待池中进入对象锁池队列中，而这些线程重新运行时它们一定 
                 * 要先要得该锁后才可能执行，这里的notifyAll是不会释放锁的，试着把下面的睡眠语 
                 * 句注释去掉，即使你已调用了notify方法，发现CreateThread中的同步块还是好 
                 * 像一直处于对象等待状态，其实调用notify方法后，CreateThread线程进入了对象锁 
                 * 池队列中了，只要它一获取到锁，CreateThread所在线程就会真真的被唤醒并运行。 
                 */  
                ThreadJdbc.class.notifyAll();  
  
                System.out.println("----" + this.getName() + " 所占用资源释放完毕，当前系统正在运行的子线程数："+  threadCounts);  
            }  
            System.out.println("<<--" + this.getName() + "运行结束");  
        }  
    }  
    
    
    static public int getThreadCounts() {  
        synchronized (ThreadJdbc.class) {  
            return threadCounts;  
        }  
    }  

}
